//
// Created by Zhangwj on 2023/9/27.
//

#include "UnaryExpNode.h"

std::unique_ptr<UnaryExpNode> UnaryExpNode::UnaryExpParser(Parser &parser) {
    // UnaryExp → PrimaryExp | Ident '(' [FuncRParams] ')'
    // | UnaryOp UnaryExp
    std::unique_ptr<UnaryExpNode> unaryExpNode = std::make_unique<UnaryExpNode>();
    if (parser.getLexType(1) == LexType::LPARENT) {
        //UnaryExp → Ident '(' [FuncRParams] ')'
        unaryExpNode->Ident = IdentNode::IdentParser(parser);
        if (parser.getLexType(0) == LexType::LPARENT) {
            parser.next();
        }
        if (parser.getLexType(0) != LexType::RPARENT) {
            //[FuncRParams]存在
            unaryExpNode->FuncRParams = FuncRParamsNode::FuncRParamsParser(parser);
        }
        if (parser.getLexType(0) == LexType::RPARENT) {
            parser.next();
        }
    } else if (parser.getLexType(0) == LexType::PLUS || parser.getLexType(0) == LexType::MINU ||
               parser.getLexType(0) == LexType::NOT) {
        //UnaryExp → UnaryOp UnaryExp
        unaryExpNode->UnaryOp = UnaryOpNode::UnaryOpParser(parser);
        unaryExpNode->UnaryExp = UnaryExpNode::UnaryExpParser(parser);
    } else {
        //UnaryExp → PrimaryExp
        unaryExpNode->PrimaryExp = PrimaryExpNode::PrimaryExpParser(parser);
    }
    return unaryExpNode;
}
